package com.zeny.express.web.mdm.user.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zeny.express.data.mdm.dto.parameter.UserSearchParameterDto;
import com.zeny.express.data.mdm.entity.UserEntity;
import com.zeny.express.framework.core.domain.Page;
import com.zeny.express.framework.log.annotation.LogAction;
import com.zeny.express.framework.shared.exception.ApplicationException;
import com.zeny.express.framework.webapp.action.BaseCrudAction;
import com.zeny.express.module.mdm.user.service.UserService;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class UserManagerAction extends BaseCrudAction {

	@Autowired
	private UserService userService;

	private UserSearchParameterDto condition = new UserSearchParameterDto();

	private UserEntity entity = new UserEntity();

	@Override
	@LogAction
	public String list() {

		/*
		 * if (true) { addActionError("测试"); addActionMessage("测试完成"); // return
		 * ERROR_TILES; return "construction"; }
		 */

		return SUCCESS;
	}

	@Override
	public String edit() {

		if (StringUtils.isEmpty(id)) { // 新增

		} else { // 修改
			entity = userService.getUserById(id);
		}

		return SUCCESS;
	}

	@Override
	@LogAction
	public String save() {

		if (StringUtils.isEmpty(entity.getId())) {
			userService.insertUser(entity);
		} else {
			userService.updateUser(entity);
		}

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String search() {

		try {
//			page.setTotalCached(true);
//			if(true) throw new ApplicationException("tt", null);
			Page<UserEntity> result = userService.searchUsersByPage(condition,
					(Page<UserEntity>) page);
			this.setResult(result);
		} catch (ApplicationException e) {
			LOGGER.error(e.getErrorCode(), e);
			addActionError(e);
			return ERROR_JSON;
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			addActionError("系统错误");
			return ERROR_JSON;
		}

		return SUCCESS;
	}

	@Override
	public String delete() {

		try {
			// if (true) throw new ApplicationException("测试", null);

			List<UserEntity> list = new ArrayList<UserEntity>(ids.size());
			for (String id : ids) {
				UserEntity user = new UserEntity();
				user.setId(id);
				list.add(user);
			}

			userService.batchDeleteUsers(list);

			return SUCCESS;
		} catch (ApplicationException e) {
			LOGGER.error(e.getErrorCode(), e);
			addActionError(e);
			return ERROR_JSON;
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			addActionError("系统错误");
			return ERROR_JSON;
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public String export() {
		
		try {
//			if (true) throw new ApplicationException(ApplicationException.UNKNOWN, null);
			
			Page<UserEntity> result = userService.searchUsersByPage(condition,
				(Page<UserEntity>) page);
			
			this.exportParams.setPage(result);
			this.exportParams.setFileName("用户列表");
			this.excelExporter.exportToFile(this.exportParams);
			
			return SYNC_EXPORT;
		} catch (ApplicationException e) {
			LOGGER.error(e.getErrorCode(), e);
			addActionError(e);
			return ERROR_TILES;
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			addActionError("系统错误");
			return ERROR_TILES;
		}
	}

	/**
	 * @param condition
	 */
	public void setCondition(UserSearchParameterDto condition) {
		this.condition = condition;
	}

	/**
	 * @return condition
	 */
	public UserSearchParameterDto getCondition() {
		return condition;
	}

	/**
	 * @return entity
	 */
	public UserEntity getEntity() {
		return entity;
	}

	/**
	 * @param entity
	 */
	public void setEntity(UserEntity entity) {
		this.entity = entity;
	}

}
